package com.ad.pet.app.account.order.fragment;

import android.app.Activity;

import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.data.injector.PerActivity;
import com.ad.pet.common.structure.mvp.AbsBasePresenter;
import com.ad.pet.models.remote.User;
import com.ad.pet.models.remote.account.order.PayCompleted;
import com.ad.pet.models.remote.account.order.PayMethodAll;
import com.ad.pet.models.remote.account.order.PayUrl;
import com.ad.pet.models.remote.account.order.SignCheck;
import com.ad.pet.models.result.ResultBean;

import javax.inject.Inject;

/**
 * Created by SEELE on 2019/2/20.
 */
@PerActivity
public class PacketInfoPresenter extends AbsBasePresenter implements PacketInfoContract.Presenter{

    private ApiService mApiService;
    private PacketInfoContract.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;

    @Inject
    public PacketInfoPresenter(ApiService apiService,
                                Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }
    @Override
    public void onCreate(PacketInfoContract.View view) {
            mView = view;
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
            mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }


    @Override
    public void onConfirmRecive(String orderId, String productId, String orderProductId) {
        mRxDisposable.add(mApiService.confirm_receipt(orderId,productId,orderProductId).subscribe(new AppSubscriber<ResultBean>(mView) {
            @Override
            public void next(ResultBean resultBean) {
                mView.onConfirmReciveSuccess(resultBean);
            }
        }));
    }



    @Override
    public void onComment(String order_id, String product_id,String order_product_id, String rating, String content, String image) {
        mRxDisposable.add(mApiService.receiving_comment( order_id,  product_id, order_product_id,  rating,  content,  image).subscribe(new AppSubscriber<ResultBean>(mView) {
            @Override
            public void next(ResultBean resultBean) {
                mView.onCommentSuccess(resultBean);
            }
        }));
    }

    @Override
    public void createOrder(String type, String amount, String pm_id, String order_sn, String uid, String pay_sign) {

        mRxDisposable.add(mApiService.create_pay(type,amount,pm_id,order_sn,uid,pay_sign).subscribe(new AppSubscriber<PayUrl>(mView) {
            @Override
            public void next(PayUrl resultBean) {
                mView.createOrderSuccess(resultBean);
            }
        }));
    }

    @Override
    public void getPayWays(String order_sn) {
        mRxDisposable.add(mApiService.get_method_list(order_sn).subscribe(new AppSubscriber<PayMethodAll>(mView) {
            @Override
            public void next(PayMethodAll resultBean) {
                mView.getPayWaysSuccess(resultBean);
            }
        }));
    }

    @Override
    public void addPayAdvice(String order_sn, String advice) {
        mRxDisposable.add(mApiService.add_comment(order_sn,advice).subscribe(new AppSubscriber<User>(mView) {
            @Override
            public void next(User resultBean) {
                mView.addPayAdvice();
            }
        }));
    }

    @Override
    public void payCompleted(String order_sn) {
        mRxDisposable.add(mApiService.pay_completed(order_sn).subscribe(new AppSubscriber<PayCompleted>(mView) {
            @Override
            public void next(PayCompleted resultBean) {
                mView.payCompletedSuccess(resultBean);
            }
        }));
    }

    @Override
    public void payCompletedOpen(String orderSn) {
        mRxDisposable.add(mApiService.pay_completed_open_red_envelope(orderSn).subscribe(new AppSubscriber<SignCheck>(mView) {
            @Override
            public void next(SignCheck resultBean) {
                mView.payCompletedOpenSuccess(resultBean);
            }
        }));
    }
}
